#include <boost/property_tree/xml_parser.hpp>
#include <nice2d/imagedescriptor.h>

IMPLEMENT_OBJECT(ImageDescriptor, Serializer)

bool ImageDescriptor::load(const std::string filename)
{
    PTree ptree;
    boost::property_tree::xml_parser::read_xml<PTree>(filename, ptree);
    if(ptree.empty())
        return false;

    ptree = ptree.get_child(getObjectName());
    deserialize(ptree);
    return !areas.empty();
}

PTree ImageDescriptor::serialize()
{
    PTree node;
    node.add("file", file);
    node.add_child("imageSize", serializeObject(imageSize));
    node.add_child("areas", serializeContainer(areas));
    return node;
}

void ImageDescriptor::deserialize(const PTree &node)
{
    areas.clear();

    file = node.get<std::string>("file");
    imageSize = deserializeVector2i(node.get_child("imageSize"));

    auto items = node.get_child("areas");

    auto itr = items.begin();
    while (itr != items.end()) {
        auto area = deserializeIntRect(itr->second);
        areas.emplace_back(area);
        itr ++;
    }
}
